﻿using System.Collections.Generic;
using System.Linq;
using SIG.Repository;
using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using SIG.Resources;
using SIG.Model.Store;
using System.Threading;
using System.Threading.Tasks;
using SIG.Resources.Admin;

namespace SIG.Service
{
    public class GoodsCategoryService :  EntityService<GoodsCategory>, IGoodsCategoryService
    {
       
        private readonly IGoodsCategoryRepository _categoryRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public GoodsCategoryService(IGoodsCategoryRepository categoryRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(categoryRepository, cacheService, loggingService)
        {           
            _categoryRepository = categoryRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }

        public IEnumerable<GoodsCategory> RecommendCategories(CancellationToken cancellationToken = default(CancellationToken))
        {
            var key = $"{EntityNames.GoodsCategory}s_Recommend";

            if (!SettingsManager.Store.EnableCaching)
            {
                return _categoryRepository.FindByAsync(p => p.Recommend && p.Active,p=>p.Importance,false, cancellationToken).Result.ToList();
            }

            if (_cacheService.IsSet(key))
            {
                return (List<GoodsCategory>)_cacheService.Get(key);

            }

           var photos = _categoryRepository.FindByAsync(p => p.Recommend && p.Active, p => p.Importance, false, cancellationToken).Result.ToList();
            _cacheService.Set(key, photos, SettingsManager.Store.CacheDuration);


            return photos;
        }

        public List<GoodsCategory> GetPagedElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        {
            if (string.IsNullOrEmpty(keyword))
            { 
                return GetPagedElements(pageIndex, pageSize, out totalCount);
            }
            
            //get list count
            var keyCount = $"{EntityNames.GoodsCategory}_ListCount__{keyword}";            
            if (SettingsManager.Store.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);

                }
                else
                {
                    totalCount = _categoryRepository.GetAllElements().Where(c=>c.Title.Contains(keyword)).Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Store.CacheDuration);
                }
            }
            else
            {
                totalCount = _categoryRepository.GetAllElements().Where(c => c.Title.Contains(keyword)).Count();
            }


            //get list
            var key = $"{EntityNames.GoodsCategory}_List_{pageIndex}_{pageSize}_{keyword}";

            List<GoodsCategory> categories;
            if (SettingsManager.Store.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    categories = (List<GoodsCategory>)_cacheService.Get(key);

                }
                else
                {
                    categories = _categoryRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), (c => c.Title.Contains(keyword)), false).ToList();
                    _cacheService.Set(key, categories, SettingsManager.Store.CacheDuration);
                }
            }
            else
            {
                categories = _categoryRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), (c => c.Title.Contains(keyword)), false).ToList();
            }

            return categories;            

        }

        public List<GoodsCategory> GetPagedElements(int pageIndex, int pageSize, out int totalCount)
        {

            //get list count
            var keyCount = $"{EntityNames.GoodsCategory}_ListCount";
            if (SettingsManager.Store.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);

                }
                else
                {
                    totalCount = _categoryRepository.GetAllElements().Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Store.CacheDuration);
                }
            }
            else
            {
                totalCount = _categoryRepository.GetAllElements().Count();
            }


            //get list
            var key = $"{EntityNames.GoodsCategory}_List_{pageIndex}_{pageSize}";

            List<GoodsCategory> categories;
            if (SettingsManager.Store.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    categories = (List<GoodsCategory>)_cacheService.Get(key);

                }
                else
                {
                    categories = _categoryRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), false).ToList();
                    _cacheService.Set(key, categories, SettingsManager.Store.CacheDuration);
                }
            }
            else
            {
                categories = _categoryRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), false).ToList();
            }

            return categories;

        }
    }
}
